package com.itheima.entry.controller.impl;

import com.itheima.entry.controller.IStatisticsController;
import com.itheima.entry.factory.StatisticsFactroy;
import com.itheima.entry.service.IStatisticsService;
import com.itheima.util.ExportExcel;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.util.*;

public class StatisticsControllerImpl implements IStatisticsController {

    private IStatisticsService statisticsService = StatisticsFactroy.getService();


    private Scanner sc = new Scanner(System.in);

    //	男生人数

    //	18,19,20三个年龄段的学生有多少人?
    //	班级总人数有多少人?
    //	班级平均年龄是多少?
    //	各个小组有多少人?
    @Override
    public void menu() {
        while (true) {
//            System.out.println("统计并导出");
            System.out.println("1.男女人数统计 2.年龄信息统计 3. 各组人数统计  4. 返回上一层");
            switch (sc.next()) {
                case "1":
                    boyAndGirl();
                    break;
                case "2":
                    ageInfo();
                    break;
                case "3":
                    sumNum();
                    break;

                case "4":

                   return;
                default:
                    System.out.println("输入有误");
                    break;
            }
        }

    }


    @Override
    public void boyAndGirl() {
        TreeMap<String, Integer> map = statisticsService.boyAndGirl();
        String[] strArr = {"序号","性别","人数"};
        exportMap("性别统计",map,strArr);
        showMap("性别",map,strArr);
        System.out.println("数据已经导出到 D:/heima_system/export/ 中");
    }

    @Override
    public void ageInfo() {
        TreeMap<String, Integer> map = statisticsService.ageInfo();
        String[] strArr = {"序号","年龄","人数"};
        exportMap("年龄统计",map,strArr);
        showMap("年龄",map,strArr);
        System.out.println("数据已经导出到 D:/heima_system/export/ 中");
    }

    @Override
    public void sumNum() {
        TreeMap<Integer, Integer> map = statisticsService.sumNum();
        String[] strArr = {"序号","组名","人数"};
        exportMap("各组人数统计",map,strArr);
        showMap("各组人数",map,strArr);
        System.out.println("数据已经导出到 D:/heima_system/export/ 中");

    }


    /*

Map导出
@ titleInfo 是文件的名字及表的标题,还有页签名
@ map 是需要写出的集合
@ str 是的各列的表头,strArr容量只能是2
 */
    private void exportMap(String titleInfo, Map<?, ?> map, String[] str) {

        HSSFWorkbook workbook = new HSSFWorkbook();                        // 创建工作簿对象

        for (int x = 1; x < 2; x++) {

            HSSFSheet sheet = workbook.createSheet(titleInfo + x);                     // 创建工作表
            HashMap<String, Object> dataMap = new HashMap<String, Object>();
            List<HashMap<String, Object>> listMap = new ArrayList<HashMap<String, Object>>();

            ArrayList<Object> list = new ArrayList<>();
            Set<?> keys = map.keySet();
            for (Object key : keys) {
                list.add(key);
            }
            for (int i = 0; i < list.size(); i++) {

                dataMap.put("id" + i, "id");
                dataMap.put("k" + i, list.get(i));
                dataMap.put("v" + i, map.get(list.get(i)));

                listMap.add(dataMap);
            }

            List<Object[]> dataList = new ArrayList<Object[]>();
            Object[] objs = null;
            for (int i = 0; i < listMap.size(); i++) {
                HashMap<String, Object> data = listMap.get(i);
                objs = new Object[str.length];
                objs[0] = data.get("id" + i);
                objs[1] = data.get("k" + i);
                objs[2] = data.get("v" + i);
                dataList.add(objs);
            }

            ExportExcel ex = new ExportExcel(titleInfo, str, dataList);
            try {
                ex.export(workbook, sheet, x, titleInfo);
            } catch (Exception e) {
                e.printStackTrace();
            }


        }


    }


    /*
    * strArr是的各列的表头,strArr容量只能是2
    * */
    private static void showMap(String title,Map<?,?> map,String[] strArr){
        System.out.println("--------------"+title+"统计信息如下---------");
        System.out.println(strArr[1]+"\t\t"+strArr[2]);
        map.forEach((k,v)-> System.out.println(k+"\t\t\t"+v));

    }


/*    public static void main(String[] args) {
        TreeMap<String, Integer> map = new TreeMap<>();
        map.put("男",25);
        map.put("女",30);
        String[] s = {"性别","人数"};

        showMap("性别",map,s);

    }*/
}
